package com.research.common;

import com.common.core.domain.BaseEntity;
import com.common.utils.SecurityUtils;
import com.common.utils.uuid.IdUtils;

import java.util.Date;

public class MetaObjectForXmlHandler {
    static public void fillForInsertGenUuid(BaseEntity entity) {
        entity.setId(IdUtils.simpleUUID());
        fillForInsert(entity);
    }

    static public void fillForInsert(BaseEntity entity) {
        Date now = new Date();
        entity.setCreateUserId(getUserId());
        entity.setCreateUserName(getUserName());
        entity.setCreateDate(now);
        entity.setLastEditUserId(getUserId());
        entity.setLastEditUserName(getUserName());
        entity.setLastEditDate(now);
    }

    static public void fillForUpdate(BaseEntity entity) {
        entity.setLastEditUserId(getUserId());
        entity.setLastEditUserName(getUserName());
        entity.setLastEditDate(new Date());
    }

    static private String getUserId(){
        try {
            return SecurityUtils.getLoginUser().getUser().getUserRealId();
        }catch(Exception ignored){
            return "获取用户id异常";
        }
    }

    static private String getUserName(){
        try {
            return SecurityUtils.getUsername();
        }catch(Exception ignored){
            return "获取用户名称异常";
        }
    }
}